#region using

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion

[ExtensionAttribute("Add text watermark to photo", "1.0.0.0", "Dave Parker")]
public class SimpleTextWaterMark : IPhotoEffect
{
	static private PhotoEffectSettings _settings = new PhotoEffectSettings(8);

	public SimpleTextWaterMark()
	{
		PhotoEffectCollection.Register("Simple Text Watermark", this);
	}

	public PhotoEffectSettings GetSettings()
	{
		if (_settings.Count != 1)
		{			
			_settings.Add(new PhotoEffectSetting("text", "Text", PhotoEffectSetting.PhotoEffectSettingType.String));
		}

		return _settings;
	}

	public System.Drawing.Image Apply(System.Drawing.Image source, PhotoEffectParameters parameters)
	{
		return ApplyTextWatermark(source, parameters.Get("text", string.Empty));
	}

	/// <summary>
	/// Converts the image to grayscale.
	/// </summary>
	/// <param name="original">The original image.</param>
	/// <returns></returns>
	public static Bitmap ApplyTextWatermark(System.Drawing.Image original, string text)
	{
		// create a blank bitmap the same size as original
		Bitmap newBitmap = new Bitmap(original.Width, original.Height);

		newBitmap.SetResolution(original.HorizontalResolution, original.VerticalResolution);

		// get a graphics object from the new image
		Graphics g = Graphics.FromImage(newBitmap);

		//Set the rendering quality for this Graphics object
		g.SmoothingMode = SmoothingMode.AntiAlias;

		// draw the original image on the new image using the grayscale color matrix
		g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);

		ApplyTextWatermark(g, text, original.Width, original.Height);

		// dispose the Graphics object
		g.Dispose();

		return newBitmap;
	}

	protected static void ApplyTextWatermark(Graphics canvas, string text, int width, int height)
	{
		//to maximize the size of the watermark we test multiple font sizes
		//to determine the largest posible font we can use for the width of the Photo.
		//Define an array of point sizes you would like to consider as possiblities
		int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

		Font myFont = null;
		SizeF mySize = new SizeF();

		//Loop through the defined sizes measuring the length of the watermark string
		//If its length in pixels is less then the image width use that Font size.
		for (int i = 0; i < 7; i++)
		{
			myFont = new Font("arial", sizes[i], FontStyle.Bold);
			mySize = canvas.MeasureString(text, myFont);

			if ((ushort)mySize.Width < (ushort)width)
			{
				break;
			}
		}

		//Since all photographs will have varying heights, determine a position 5% from the bottom of the image
		int yPixlesFromBottom = (int)(height * .05);

		//Now that we have a point size use the watermark text height 
		//to determine a y-coordinate to draw the string of the photograph
		float yPosFromBottom = ((height - yPixlesFromBottom) - (mySize.Height / 2));

		//Determine its x-coordinate by calculating the center of the width of the image
		float xCenterOfImg = (width / 2);

		//Define the text layout by setting the text alignment to centered
		StringFormat stringFormat = new StringFormat();
		stringFormat.Alignment = StringAlignment.Center;

		//define a Brush which is semi trasparent black (Alpha set to 153)
		SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

		//Draw the watermark string
		canvas.DrawString(text, //string of text
			myFont,                                                  //font
			semiTransBrush2,                                         //Brush
			new PointF(xCenterOfImg + 1, yPosFromBottom + 1),        //Position
			stringFormat);

		//define a Brush which is semi trasparent white (Alpha set to 153)
		SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

		//Draw the text string a second time to create a shadow effect
		//Make sure to move this text 1 pixel to the right and down 1 pixel
		canvas.DrawString(text,										 //text
			myFont,                                                  //font
			semiTransBrush,                                          //Brush
			new PointF(xCenterOfImg, yPosFromBottom),                //Position
			stringFormat);                                           //Text alignment
	}

}
